<template>
  <div class="pad-20 bgc-default">
    <div class="tab-sticky">
      <el-tabs v-model="queryParams.goodsTagBusType" @tab-click="changTab">
        <el-tab-pane label="零售商品" name="1"></el-tab-pane>
        <el-tab-pane label="批发商品" name="2"></el-tab-pane>
      </el-tabs>
    </div>
    <div class="pad-20 bg-color-white mar-t-20">
      <div>
        <el-button @click="addGoodsTag" type="primary">新建标签</el-button>
      </div>
      <!--顶部搜索 -->
      <div class="top-operate-default mar-t-20">
        <div class="search-item">
          <label>搜索标签：</label>
          <el-input style="width: 200px;" v-model.trim="queryParams.goodsTagName" placeholder="请输入标签名称"></el-input>
          <el-button-group class="mar-l-20">
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button plain @click="resetSearch">重置</el-button>
          </el-button-group>
        </div>
      </div>
      <!-- 内容列表 -->
      <div class="table-container">
        <el-table ref="multipleTable" :data="goodsTagList" v-loading="listLoading" element-loading-text="加载中">
          <el-table-column prop="goodsTagName" label="标签名称" />
          <el-table-column prop="goodsTagNum" label="商品数">
            <template slot-scope="scope">
              {{scope.row.goodsList != undefined && scope.row.goodsList != null ? scope.row.goodsList.length : 0}}
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" />
          <el-table-column min-width="200px" label="操作" align="right" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" size="medium" @click="showGoodsDetailList(scope.row)">商品明细</el-button>
              <el-button type="text" size="medium" @click="editGoodsTag(scope.row)">编辑</el-button>
              <el-popconfirm title="您确定删除吗？" @onConfirm="deleteGoodsTag(scope.row)">
                <el-button slot="reference" class="mar-l-10" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <pagination :total="total" :page.sync="queryParams.currentPage" :limit.sync="queryParams.size"
        @pagination="getList" />
    </div>
    <el-dialog :title="editGoodsTagDialogTitle" v-el-drag-dialog :visible.sync="editGoodsTagDialogVisible"
      :close-on-press-escape="true" :center="true" width="600px">
      <el-form ref="goodsTagForm" :model="goodsTagForm" label-position="right" :rules="rules" label-width="120px">
        <el-form-item label="标签名称" prop="goodsTagName">
          <el-input v-model="goodsTagForm.goodsTagName" class="width-per-70" maxlength="30" placeholder="请输入标签名称">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editGoodsTagDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveGoodsTagInfo">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="选择商品" :visible.sync="selectGoodsVisible" :close-on-press-escape="true" :center="true"
      v-if="selectGoodsVisible" width="1200px">
      <div class="scroll-layer-box">
        <goods-select ref="goodsSelectComponent" :goodsTagObj="selectedGoodsTagObj"
          @saveDetail="saveGoodDetailCallBack"></goods-select>
      </div>
      <div class="dialog-footer mar-b-20">
        <el-button type="primary" @click="saveSelectGoods">保存</el-button>
        <el-button plain @click="selectGoodsVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import goodsSelect from './components/goods-select'
  import elDragDialog from '@/directive/el-dragDialog'
  import {
    pageGoodsTag,
    saveGoodsTag,
    deleteTag
  } from '@/api/goods/goodsTag'
  export default {
    components: {
      goodsSelect
    },
    directives: {
      elDragDialog
    },
    created() {
      this.getList()
    },
    data() {
      return {
        queryParams: {
          currentPage: 1,
          size: 10,
          goodsTagBusType: '1',
          enterpriseId: this.getEnterpriseId()
        },
        goodsTagList: [],
        listLoading: false,
        editGoodsTagDialogVisible: false,
        total: 0,
        editGoodsTagDialogTitle: '新建标签',
        // 商品标签表单
        goodsTagForm: {
          goodsTagId: '',
          goodsTagName: '',
          goodsTagBusType: '1'
        },
        rules: {
          goodsTagName: [{
            required: true,
            message: '请输入标签名称',
            trigger: 'blur'
          }]
        },
        selectedGoodsTagObj: [],
        selectGoodsVisible: false
      }
    },
    methods: {
      addGoodsTag() {
        this.editGoodsTagDialogTitle = '新增商品组'
        this.goodsTagForm.goodsTagId = ''
        this.goodsTagForm.goodsTagName = ''
        if (this.$refs['goodsTagForm']) this.$refs['goodsTagForm'].resetFields()
        this.editGoodsTagDialogVisible = true
      },
      changTab(tab) {
        this.goodsTagForm.goodsTagBusType = tab.name
        this.resetSearch()
      },
      handleSearch() {
        this.queryParams.currentPage = 1
        this.getList()
      },
      resetSearch() {
        this.queryParams.currentPage = 1
        this.queryParams.goodsTagName = ''
        this.getList()
      },
      getList() {
        this.listLoading = true
        pageGoodsTag(this.queryParams).then(response => {
          if (response.isError) return
          this.goodsTagList = response.data.records
          this.total = response.data.total
          this.listLoading = false
        })
      },
      showGoodsDetailList(row) {
        this.selectedGoodsTagObj = Object.assign({}, row)
        this.selectGoodsVisible = true
      },
      editGoodsTag(row) {
        this.editGoodsTagDialogTitle = '编辑' + row.goodsTagName
        this.editGoodsTagDialogVisible = true
        this.goodsTagForm.goodsTagId = row.goodsTagId
        this.goodsTagForm.goodsTagName = row.goodsTagName
        this.goodsTagForm.goodsTagBusType = row.goodsTagBusType
      },
      deleteGoodsTag(row) {
        var ids = []
        var dupCheck = false
        var dupCheckMsg = ''
        if (row.goodsTagId) {
          ids.push(row.goodsTagId)
          if (row.goodsList !== undefined && row.goodsList !== null) {
            dupCheck = row.goodsList.length > 0
          }
          dupCheckMsg = row.goodsTagName
        } else {
          this.multipleSelection.forEach(o => {
            ids.push(o.goodsTagId)
            if (!dupCheck) {
              if (row.goodsList !== undefined && row.goodsList !== null) {
                dupCheck = o.goodsList.length > 0
              }
              dupCheckMsg = o.goodsTagName
            }
          })
        }
        if (ids.length === 0) {
          this.msgError('请选择要删除的商品标签')
          return
        }
        if (dupCheck) {
          this.msgError('该标签组下存在商品数据无法删除')
          return
        }
        this.$confirm('商品标签删除后不可恢复，确定要删除？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          if (dupCheck) {
            this.$confirm('商品标签:【' + dupCheckMsg + '】存在关联商品，删除后不可恢复，确定要删除？', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.doDelete(ids)
            }).catch(() => {})
          } else {
            this.doDelete(ids)
          }
        }).catch(() => {})
      },
      // 删除事件
      doDelete(ids) {
        deleteTag({
          goodsTagIdList: ids
        }).then(response => {
          if (response.isError) return
          this.msgSuccess('删除成功')
          this.getList()
        })
      },
      saveGoodsTagInfo() {
        this.$refs['goodsTagForm'].validate((valid) => {
          if (valid) {
            this.goodsTagForm.enterpriseId = this.getEnterpriseId()
            saveGoodsTag(this.goodsTagForm).then((response) => {
              if (response.isError) return
              this.msgSuccess('保存成功!')
              this.editGoodsTagDialogVisible = false
              this.getList()
            })
          }
        })
      },
      saveSelectGoods() {
        this.$refs.goodsSelectComponent.saveSelectGoods()
      },
      // 商品明细回调事件
      saveGoodDetailCallBack(refreshFlag) {
        this.selectGoodsVisible = false
        if (refreshFlag) this.getList()
      }
    }
  }
</script>

<style>
</style>
